package com.suse.service.admin.cms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suse.common.base.model.param.PageQueryParam;
import com.suse.common.config.response.ApiException;
import com.suse.common.config.response.R;
import com.suse.service.admin.cms.model.entity.Ad;
import com.suse.service.admin.cms.mapper.AdMapper;
import com.suse.service.admin.cms.model.param.AdSaveParam;
import com.suse.service.admin.cms.model.param.AdUpdateParam;
import com.suse.service.admin.cms.model.vo.AdVo;
import com.suse.service.admin.cms.service.AdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 广告推荐 服务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-17
 */
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements AdService {

    @Resource
    private AdMapper adMapper;

    @Override
    public R getAdList(PageQueryParam pageQueryParam) {
        Long current = pageQueryParam.getCurrent();
        Long size = pageQueryParam.getSize();
        Page<AdVo> page = new Page<>(current,size);
        page = adMapper.getAdList(page);
        return R.put("total",page.getTotal()).put("list",page.getRecords()).success();
    }

    @Override
    public R saveAd(AdSaveParam adSaveParam) {
        Ad ad = BeanUtil.copyProperties(adSaveParam, Ad.class);
        if (save(ad)) {
            return R.success("添加成功");
        }else {
            throw new ApiException("添加失败！");
        }
    }

    @Override
    public R updateAd(AdUpdateParam adUpdateParam) {
        Ad ad = BeanUtil.copyProperties(adUpdateParam, Ad.class);
        if (updateById(ad)) {
            return R.success("修改成功");
        }else {
            throw new ApiException("修改失败！");
        }
    }

    @Override
    public R getAdInfo(String id) {
        return R.success(getById(id));
    }

}
